import 'package:flutter/material.dart';
import 'package:flutter_hx/model/address.dart';
import 'package:flutter_hx/services/app_theme_services.dart';
import 'package:flutter_hx/theme/font_size.dart';
import 'package:flutter_hx/widgets/address_item_common.dart';
import 'package:get/get.dart';

import 'address_list_logic.dart';

class AddressListPage extends GetView<AddressListLogic> {
  const AddressListPage({Key? key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      backgroundColor: const Color(0xFFF7F8FA),
      appBar: AppBar(
        backgroundColor: Colors.white,
        elevation: 0,
        automaticallyImplyLeading: true,
        iconTheme: const IconThemeData(color: Colors.black),
        centerTitle: true,
        title: Text("地址管理", style: AppThemeServices.textTheme.appBarTitle),
        actions: [
          TextButton(
              onPressed: () => controller.switchManageMode(),
              child: GetBuilder<AddressListLogic>(
                id: controller.managingButtonId,
                builder: (controller) {
                  return Text(!controller.isManaging.value ? "管理" : "完成",
                      style: const TextStyle(
                          fontSize: FontSize.pt14, color: Color(0xFF323233)));
                },
              ))
        ],
      ),
      body: Stack(
        children: [
          Obx(() {
            var addressList = controller.addressList.value;
            var isManaging = controller.isManaging.value;
            if (addressList.isNotEmpty) {
              return ListView.separated(
                  itemBuilder: (context, index) {
                    Address data = addressList[index];
                    return !isManaging
                        ? AddressItem.common(
                            data: data,
                            isFirst: index == 0,
                            isLast: index == addressList.length - 1,
                            onTap: () => controller.toEdit(data),
                          )
                        : AddressItem.managing(
                            data: data,
                            isFirst: index == 0,
                            isLast: index == addressList.length - 1,
                            onTap: () => controller.toEdit(data),
                            onSetDefault: () =>
                                controller.setDefaultAddress(data.id, index),
                            onDelete: () =>
                                controller.deleteAddress(data.id),
                          );
                  },
                  separatorBuilder: (context, index) => !isManaging
                      ? Container(
                          margin: const EdgeInsets.symmetric(horizontal: 10),
                          height: 25,
                          color: Colors.white,
                        )
                      : const SizedBox(
                          height: 10,
                        ),
                  itemCount: addressList.length);
            }
            return const SizedBox.expand();
          }),
          Positioned(
              bottom: 20,
              child: SizedBox(
                width: Get.width,
                child: InkWell(
                  onTap: () => controller.toAdd(),
                  child: Container(
                    margin: const EdgeInsets.symmetric(horizontal: 10),
                    decoration: BoxDecoration(
                        color: const Color(0xFF00C3B3),
                        borderRadius: BorderRadius.circular(45)),
                    height: 43,
                    child: const Center(
                      child: Text(
                        "+添加收货地址",
                        style: TextStyle(
                            fontSize: FontSize.pt15, color: Colors.white),
                        textAlign: TextAlign.center,
                      ),
                    ),
                  ),
                ),
              ))
        ],
      ),
    );
  }
}
